home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 361_01 / basicstr.c < prev    next >
C/C++ Source or Header  |  1991-09-18  |  2KB  |  94 lines

  1.  
  2. /* BasicStr.c --> A Collection of String Tools
  3.  *
  4.  * Author: J.Ekwall                                     13 September 91
  5.  *
  6.  * Copyrighted to the Public Domain.  Unlimited Distribution Authorized.
  7.  *
  8.  * User Assumes All Risks/Liabilities.
  9.  *
  10.  * Last Update: 18 September 91/EK
  11.  */
  12.  
  13. #include <ctype.h>
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <stdek.h>
  17. #include <gadgets.h>
  18.  
  19. void ExpandTabs(char *Text)
  20. {
  21.     int i;
  22.     
  23.     for (i = 0; Text[i]; i++)
  24.        if (Text[i] IS HT) { 
  25.           Text[i++] = SPACE; OpenStr(Text + i, (8 - (i % 8)) % 8); --i; }
  26. }
  27.  
  28. void PadLeft(char *Text, int ch, int N)
  29. {
  30.     if (N > strlen(Text)) {
  31.        strrev(Text); PadRight(Text, ch, N); strrev(Text); }
  32. }
  33.  
  34. void PadRight(char *Text, int ch, int N)
  35. {
  36.     if ((N -= strlen(Text)) > 0) {
  37.        for (Text += strlen(Text); N--; ) *Text++ = ch; *Text = NULL; }
  38. }
  39.  
  40. void PadEnds(char *Text, int ch, int N)
  41. {
  42.    if (*Text) PadLeft(Text, ch, (N + strlen(Text)) / 2);
  43.    PadRight(Text, ch, N);
  44. }
  45.  
  46. void RepeatChr(char *Target, int ch, int N)
  47. { *Target = NULL; PadRight(Target, ch, N); }
  48.  
  49. void Remove(char *Text, int ch)
  50. { while ((Text = strchr(Text, ch)) != NULL) strcpy(Text, Text + 1); }
  51.  
  52. int Sar(char *Text, char *This, char *That, int StopAt)
  53. {
  54.     int i, j, k, n = 0;
  55.  
  56.     if (!StopAt || (i = strlen(This)) IS 0) return 0; j = strlen(That);
  57.     if (strstr(That, This) != NULL) return 0;
  58.     for (Text = strstr(Text, This); n < StopAt && Text != NULL; n++) {
  59.        strcpy(Text, Text + i);
  60.        if (j) { Strrcpy(Text+j, Text); memcpy(Text, That, j); }
  61.        Text = strstr(Text, This);
  62.     }
  63.     return n;
  64. }
  65.  
  66. void Strrcpy(char *Target, char *Source)
  67. /* Target may be Inside Source */
  68. {
  69.     int N;
  70.  
  71.     for (N = strlen(Source), Source += N, Target += N++; N--; )
  72.        *Target-- = *Source--;
  73. }
  74.  
  75. int Tally(char *Text, int ch)
  76. {
  77.     int n;
  78.  
  79.     for(n = 0; (Text = strchr(Text, ch)) != NULL; n++) Text++;
  80.     return n;
  81. }
  82.  
  83. char *TrimStr(char *Text, int Flag)
  84. {
  85.     int i;
  86.  
  87.     if (Flag > 2 || Flag < 0 || !*Text) Flag = 0;
  88.     if (!Flag || Flag > 1)
  89.        while ((i = strlen(Text) - 1) >= 0 && isspace(Text[i])) Text[i] = NULL;
  90.     if (Flag < 2 && (i = strspn(Text, " \t\n\f")) > 0) strcpy(Text, Text + i);
  91.     return Text;
  92. }
  93.  
  94.